#ifdef USAGECOROUTINE
#ifdef __linux__
#include "coroutineSchdule.h"

static Scheduler* g_Scheduler = NULL;
struct timeval begin;

Scheduler *GetInstance_crt()
{
    if (g_Scheduler == NULL)
    {
        g_Scheduler = (Scheduler*)malloc(sizeof(Scheduler));
    }
    return g_Scheduler;
}

void scheduler_run()
{
    int epfd = SetEpoll();
    struct epoll_event evs[1024] = {0};
    Scheduler* s = GetInstance_crt();
    s->epfd = epfd;

    gettimeofday(&begin,NULL);

    while (1)
    {
        int nready = epoll_wait(epfd,evs,1024,100);
#ifdef PRINTFCMODE
        printf("epollin:%d\n",nready);
#endif
        for (int i = 0; i < nready; i++)
        {
            int curid = evs[i].data.fd;
            Coroutine *co = s->coroutines[curid];
            
            if (co->state == COROUTINE_READY || co->state == COROUTINE_SUSPEND) 
            {
                epoll_ctl(epfd,EPOLL_CTL_DEL,co->fd,NULL);
#ifdef PRINTFCMODE
                printf("del----co->fd:%d\n",co->fd);
#endif
                coroutine_resume(curid);
            }
        }
    }
}

void* coroutine_start(void *arg)
{
    Scheduler* s = GetInstance_crt();
    memset(s, 0, sizeof(s));
    s->current_id = -1;

    scheduler_run();

    // 释放资源
    for (int i = 0; i < s->id_max; i++) 
    {
        if(s->coroutines[i]) 
            free(s->coroutines[i]);
    }
    return NULL;
}
#endif

#endif